#!/usr/bin/env python3
#
#sklearn 中泰坦尼克号 kaggle 案例 
#
# 逻辑回归 
# 依赖数据较多，进行拟合
#

import pandas
from sklearn.linear_model import LogisticRegression
from sklearn import cross_validation   #<---这部分已经在2.0中被废弃掉，使用model_selection 代替cross_validation
import numpy as np

titanic = pandas.read_csv("train.csv")

#Age列讲缺失的补充，原有的数据均值进行填充
titanic["Age"] = titanic["Age"].fillna(titanic["Age"].median())

#将str 转成 int 
titanic.loc[titanic["Sex"] == "male","Sex"] = 0
titanic.loc[titanic["Sex"] == "female","Sex"] = 1
titanic["Embarked"] = titanic["Embarked"].fillna('S')
titanic.loc[titanic["Embarked"] == "S","Embarked"] = 0
titanic.loc[titanic["Embarked"] == "C","Embarked"] = 1
titanic.loc[titanic["Embarked"] == "Q","Embarked"] = 2

predictors = ["Pclass","Sex","Age","SibSp","Parch","Fare","Embarked"]

alg = LogisticRegression(random_state = 1)
#设定逻辑
scores = cross_validation.cross_val_score(alg,titanic[predictors],titanic["Survived"],cv = 3)

print("预测：",scores.mean())